import {
  getJieCheDanList
} from '../../../utils/networkRequest.js'
// pages/jiechedanbox/main/main.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [{
        id: '1',
        title: '服务中'
      },
      {
        id: '2',
        title: '未完成'
      }, {
        id: '3',
        title: '已完成'
      }
    ],
    selectedId: '1',
    selectedTitle: '服务中',

    orderList: [], //订单列表
    page: 1,
    last_page: -1, //最后一页
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function(options) {
    let self = this;
    let { selectedId,page} = this.data
    let statusNum = 0;
    if (~~selectedId == 1 || ~~selectedId==2){
      statusNum = 0;
    }else{
      statusNum = 1;
    }
    getJieCheDanList(statusNum,page, (r) => {
      if (r.code == 0) {
        debugger;
        self.setData({
          orderList: r.data,
           last_page: r.pager.last_page
        })
      }
    })
  },
  //切换tab
  handleTabChange: function(tabId) {
    let self = this; 
    let val = tabId.detail
    if (val == 1) {
      getJieCheDanList(0, 1, (r) => {
        if (r.code == 0) {
          self.setData({
            orderList: r.data,
            selectedTitle: "服务中",
            selectedId:1,
            page:1,
            last_page: r.pager.last_page
          })
        }
      })
    } else if (val == 2) {
      getJieCheDanList(0, 1, (r) => {
        if (r.code == 0) {
          self.setData({
            orderList: r.data,
            selectedTitle: "未完成",
            selectedId:2,
            page: 1,
            last_page: r.pager.last_page
          })
        }
      })
    } else if (val == 3) {
      getJieCheDanList(1, 1, (r) => {
        if (r.code == 0) {
          self.setData({
            orderList: r.data,
            selectedTitle: "已完成",
            selectedId:3,
            page: 1,
            last_page: r.pager.last_page
          })
        }
      })
    }
  },

  //下啦刷新
  onPullDownRefresh: function () {
    // let self = this;
    // let {
    //   last_page,
    //   selectedId,
    // } = this.data;
    // getChecks(selectedId, 1, (r) => {
    //   self.setData({
    //     checksInfo: r.data,
    //     page: 1,
    //     last_page: r.pager.last_page
    //   })
    //   // wx.stopPullDownRefresh()
    // })



    let self = this;
    let { selectedId, page } = this.data
    let statusNum = 0;
    if (~~selectedId == 1 || ~~selectedId == 2) {
      statusNum = 0;
    } else {
      statusNum = 1;
    }
    getJieCheDanList(statusNum, 1, (r) => {
      if (r.code == 0) {
        debugger;
        self.setData({
          orderList: r.data,
          page:1,
          last_page: r.pager.last_page
        })
        wx.stopPullDownRefresh()
      }
    })
  },
  //上拉加载
  onReachBottom: function () {
    var self = this;
    let {
      page,
      last_page,
      selectedId,
    } = this.data;

    if (last_page <= page) {
      wx.showToast({
        icon: "none",
        title: '没有更多数据了.'
      })
    } else {
      page = page + 1; 
      let statusNum = 0;
      if (~~selectedId == 1 || ~~selectedId == 2) {
        statusNum = 0;
      } else {
        statusNum = 1;
      }
      getJieCheDanList(statusNum, page, (r) => {
        if (r.code == 0) { 
          self.setData({
            orderList: self.data.orderList.concat(r.data),
            page: page,
            last_page: r.pager.last_page
          })
        }
      })
    }

  },
  goOrderDetail: function(e) {
    let id = e.currentTarget.dataset.id
    debugger;
    wx.navigateTo({
      url: "../orderDetail/orderDetail?id="+id
    })
  },
  goCheckDetail: function(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: "../checkDetail/checkDetail?id=" + id
    })
  }
})